<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" :default-expand-all="true">
          <template v-slot="scope">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon :icon-class="scope.node.expanded?'jian':'jia'" />
                <span>{{ scope.data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ scope.data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(scope.data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(scope.data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hDel(scope.data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="isEdit?'编辑部门':'添加部门'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="hDialogClose"
      >
        <deptDialogt
          ref="deptDialog"
          :origin-list="originList"
          :pid="curId"
          :is-edit="isEdit"
          @success="hSuccess"
          @close="hClose"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import deptDialogt from './deptDialog.vue'
import { getDepartments, delDepartment } from '@/api/departments'
import { treeList } from '@/utils/index'

export default {
  components: { deptDialogt },
  data() {
    return {
      originList: [],
      curId: '',
      // 是否显示弹层
      showDialog: false,
      // 依赖一份树形数据
      list: [],
      isEdit: false
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 只要 dialog showDialog从true变成false，这里就会执行
    hDialogClose() {
      // 调用子组件中的方法，来清空子组件中的表单
      this.$refs.deptDialog.resetForm()
      console.log('dialog, 关闭....')
    },

    hEdit(id) {
      // 显示弹窗
      this.curId = id
      this.showDialog = true
      this.isEdit = true
      // 获取子组件的引用
      this.$nextTick(() => {
        console.log('获取子组件的引用', this.$refs.deptDialog)
        console.log('当前的curId', id)
        console.log('获取到子组件中的id--从父传入的', this.$refs.deptDialog.id)
        // 调用子组件的方法
        this.$refs.deptDialog.loadGetDepartment()
      })
    },

    async loadDepartments() {
      const res = await getDepartments()
      console.log('人员信息', res)
      // shift 从数组头部取出一个元素
      const arr = res.data.depts.shift()
      this.originList = res.data.depts.map(item => { return { id: item.id, name: item.name, code: item.code, pid: item.pid } })
      console.log(arr)
      // 数组转树形
      this.list = treeList(res.data.depts)
    },

    hAdd(id) {
      this.showDialog = true
      this.curId = id
    },

    // 收到子组件中的success事件
    hSuccess() {
      // 关闭弹层，再次更新数据
      this.showDialog = false
      this.loadDepartments()
    },

    // 收到子组件中的close事件
    hClose() {
      // 关闭弹层，再次更新数据
      this.showDialog = false
    },

    // 用户点击删除
    async doDel(id) {
      try {
        const res = await delDepartment(id)
        console.log(res)
        // 1. 提示
        this.$message({ type: 'success', message: '删除成功!' })
        // 2. 更新数据
        this.loadDepartments()
      } catch (err) {
        console.log(err)
        this.$message({ type: 'error', message: '删除失败' })
      }
    },
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 做删除
        this.doDel(id)
      }).catch(() => {

      })
    }
  }
}
</script>

